<!-- 定时任务 -->
<template>
  <div class="item-one" ref="itemOne">
    <div class="title">
        <span>定时任务</span>
         <div>
            <el-button type="primary" @click="addopen">新增任务</el-button>
        </div>
    </div>
    <div class="container">
      <div class="screeningBox">
        <div class="leftBox">
          <div class="leftitem">
            
            <el-form label-width="80px">
                <el-input class="merchants" v-model="name" placeholder="任务名称"></el-input>
              <el-select class="servicename1" v-model="state"  placeholder="任务状态" @change="stateval">
                <el-option keys="1" label="启动" :value="1" ></el-option>
                <el-option keys="2" label="暂停" :value="2" ></el-option>
              </el-select>
            </el-form>
          </div>
          <div class="leftitem">
          </div>
        </div>
        <div class="rightBox">
          
          <div class="rightitem">
              <el-button icon="el-icon-search" type="primary" @click="soso" :disabled="isDisable">搜索</el-button>
              <el-button icon="el-icon-refresh" type="primary" @click="reset">重置</el-button>
          </div>
        </div>
      </div>

      <div class="tableBox">
        <el-table
          ref="multipleTable"
          tooltip-effect="dark"
          style="width: 100%"
          :data="tableData"
          :header-cell-style="tableHeaderColor"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column align="center" type="index" label="任务编号"></el-table-column>
          <el-table-column prop="jobName" align="center" label="任务名称"></el-table-column>
          <el-table-column prop="beanName" align="center" label="任务类名"></el-table-column>
          <el-table-column prop="methodName" align="center" label="方法名称"></el-table-column>
          <el-table-column align="center" width="300" label="执行表达式">
            <template slot-scope="scope">
              <p class="table_p">{{scope.row.cronExpression}}</p>
            </template>
          </el-table-column>
          <el-table-column prop="creatorRoleName" align="center" label="状态">
            <template slot-scope="scope">
                <span v-if="scope.row.status == 1">启动</span>
                <span v-if="scope.row.status == 2">暂停</span>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" align="center" label="创建时间"></el-table-column>
          <el-table-column align="center" label="操作" width="120">
            <template slot-scope="scope">
              <el-button type="text" v-if="scope.row.status == 1" @click="stop(scope.row.id)">暂停</el-button>
              <el-button type="text" v-if="scope.row.status == 2" @click="start(scope.row.id)">启动</el-button>
              <el-button type="text" @click="updateTask(scope.row)">详情</el-button>
              <el-button type="text" @click="deleteTask(scope.row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination">
          <el-pagination
            background
            @current-change="handleCurrentChange"
            @size-change="sizechange"
            :page-sizes="[5,10,20,30,40,50]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :current-page.sync="cur_page"
          ></el-pagination>
        </div>
      </div>
      <br>
      <br>
    </div>
    <!-- 新增任务 -->
      <el-dialog v-dialogDrag :title="classs == false?'新增任务':'修改任务'" :visible.sync="add" width="45%" :close-on-click-modal="false">
        <el-form
          status-icon
          ref="facility"
          label-width="130px"
          class="demo-dynamic wid100"
        >
          <el-form-item label="定时任务名称:" required>
            <el-input type="text" placeholder="" autocomplete="off" v-model="addtext.jobName" :disabled="classs"></el-input>
          </el-form-item>
          <el-form-item label="表达式:" required>
            <el-input type="text" placeholder="" v-model="addtext.cronExpression"></el-input>
          </el-form-item>
          <el-form-item label="类名:" required>
            <el-input type="text" placeholder="" v-model="addtext.beanName" :disabled="classs"></el-input>
          </el-form-item>
          <el-form-item label="方法名称:" required>
            <el-input type="text" placeholder="" v-model="addtext.methodName" :disabled="classs"></el-input>
          </el-form-item>
          <el-form-item label="任务描述:">
            <el-input type="textarea" placeholder="" v-model="addtext.detail" :disabled="classs"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="add = false">取消</el-button>
          <el-button type="primary" @click="addtask">确定</el-button>
        </span>
      </el-dialog>
  </div>
</template>

<script>
import { joblist,jobadd,jobupdate,jobpause,jobstart,jobdelete } from "@/api/api";
export default {
  name: "item-one",
  data: function() {
    return {
      loading: false,
      isDisable: false,
      tableData: [],
      cur_page: 1,
      cur_size: 10,
      total: 0,
      name:'',
      state:'',
      add: false,
      addtext:{},
      classs:'',
      taskid:''
    };
  },
  created() {
    this.userinfo = JSON.parse(sessionStorage.getItem('userInfo'))
    this.joblist()
  },
  methods: {
    //修改thead 背景色
      tableHeaderColor({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0) {
        return 'background-color: #F0F0F0;font-weight: 500;font-size:14px;color:#000;'
      }
    },
    // 刷新
    // refresh() {
    //   this.getInventory()
    // },
    // 分页导航
    handleCurrentChange(val) {
      this.cur_page = val;
      this.joblist()
    },
    // 分页数量
    sizechange(val) {
      this.cur_size = val;
      this.joblist()
    },
    //多选框
    // handleSelectionChange(val) {
    //   this.multipleSelection = val;
    // },
    //  重置
    reset() {
      this.name = ''
      this.status = ''
      this.state = ''
    },
    // 搜索
    soso() {
      this.cur_page = 1
      this.joblist()
    },
    stateval(val){
        this.status = val
    },
    // 打开新增定时任务
    addopen(){
      this.add = true
      this.addtext = {}
      this.classs = false
    },
    //获取任务列表数据
    joblist(){
      this.loading = true
      this.isDisable = true
      joblist({
        jobName: this.name,
        page: this.cur_page,
        size: this.cur_size,
        status: this.status
      }).then(res => {
        if (res.code == 405) {
          sessionStorage.removeItem("token");
          this.$router.push("/login");
        } else if(res.code == 200){
          this.isDisable = false
          this.loading = false
          this.tableData = res.data.rows
          this.total = res.data.total
        }else if(res.code == 500){
          this.isDisable = false
          this.loading = false
          this.$message.error(res.message);
        }
      })
    },
   
    //删除任务
    deleteTask(v){
      this.$confirm(`是否确定删除该任务？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
        jobdelete({},v).then(res => {
          if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
          } else if(res.code == 200){
            this.$message({
                  message: '删除成功',
                  type: 'success'
                });
            this.joblist()
          }else if(res.code == 500){
            this.$message.error(res.message);
          }
        })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    // 新增任务
    addtask(){
      if(this.classs == 0){
        jobadd({
            jobName: this.addtext.jobName,
            cronExpression: this.addtext.cronExpression,
            beanName: this.addtext.beanName,
            methodName: this.addtext.methodName,
            detail: this.addtext.detail
        }).then(res => {
            if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
            } else if(res.code == 200){
                this.$message({
                  message: "新增任务成功",
                  type: "success"
                });
                this.add = false
                this.addtext ={}
                this.cur_page = 1
                this.joblist()
            }else{
                this.$message.error(res.message);
            }
        })
      }else if (this.classs == 1){
        jobupdate({
            id: this.taskid,
            cronExpression: this.addtext.cronExpression,
        }).then(res => {
            if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
            } else if(res.code == 200){
                this.$message({
                  message: "修改任务成功",
                  type: "success"
                });
                this.add = false
                this.addtext ={}
                this.cur_page = 1
                this.joblist()
            }else{
                this.$message.error(res.message);
            }
        })
      }
        
    },
    // 暂停任务
    stop(id){
      this.$confirm(`确定暂定任务？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
        jobpause({},id).then(res => {
            if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
            } else if(res.code == 200){
                this.$message({
                  message: "任务已暂停",
                  type: "success"
                });
                this.cur_page = 1
                this.joblist()
            }else{
                this.$message.error(res.message);
            }
        })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    // 启动任务
    start(id){
      this.$confirm(`确定启动任务？`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
      }).then(() => {
        jobstart({},id).then(res => {
            if (res.code == 405) {
            sessionStorage.removeItem("token");
            this.$router.push("/login");
            } else if(res.code == 200){
                this.$message({
                  message: "任务已启动",
                  type: "success"
                });
                this.cur_page = 1
                this.joblist()
            }else{
                this.$message.error(res.message);
            }
        })
      })
      .catch(() => {
          this.$message({
              type: "info",
              message: "已取消"
          });
      });
    },
    // 打开编辑任务
    updateTask(val){
        this.add = true
        this.classs = true
        this.taskid = val.id
        this.addtext = {
          jobName:val.jobName,
          cronExpression: val.cronExpression,
          beanName: val.beanName,
          methodName: val.methodName,
          detail: val.detail
        }
    }
  }
};
</script>
<style scoped lang='less'>
.table_p{
    text-indent: 24px;
    text-align: center;
    letter-spacing:2px;
}
.title {
  background: #fff;
  padding: 10px 10px;
  font-size: 18px;
  font-weight: 500;
  display: flex;
  justify-content: space-between;
}
.screeningBox {
  width: 100%;
  display: flex;
}
.leftBox,
.rightBox {
  width: 50%;
  padding: 0 15px;
  box-sizing: border-box;
}
.leftBox > .leftitem {
  margin-bottom: 15px;
}
.rightBox > .rightitem {
  margin-bottom: 15px;
}
.tableBox {
  margin-top: 20px;
}
.red {
  color: #f9607d;
}
.blue {
  color: #01aaef;
}
.stitle {
  padding: 5px 0;
  font-size: 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  i {
    margin-left: 10px;
    margin-right: 5px;
  }
  .swarning {
    color: red;
  }
  .warning {
    color: #f9607d;
  }
}
.merchants{
  width: 50%;
}
</style>